package my.syncfilter.ftp;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.context.properties.EnableConfigurationProperties;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;

/**
 * FTPClient配置类，封装了FTPClient的相关配置
 *
 */
//@Configuration
//@EnableConfigurationProperties(FtpClientProperties.class)
public class FtpClientConfigure {

    private FtpClientProperties ftpClientProperties;

    //@Autowired
    public void setFtpClientProperties(FtpClientProperties ftpClientProperties) {
        this.ftpClientProperties = ftpClientProperties;
    }

    //@Bean
    public FtpClientFactory getFtpClientFactory() {
        return new FtpClientFactory(ftpClientProperties);
    }

    //@Bean
    //@Autowired
    public GenericObjectPoolConfig genericObjectPoolConfig(FtpClientProperties ftpClientProperties){
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
        //maxIdle最大空闲资源数
        genericObjectPoolConfig.setMaxIdle(ftpClientProperties.getMaxIdle());
        genericObjectPoolConfig.setMinIdle(ftpClientProperties.getMinIdle());
        //maxTotal 允许创建对象的最大数量
        genericObjectPoolConfig.setMaxTotal(ftpClientProperties.getMaxTotal());
        // 池中的闲置对象是否由逐出器验证。无法验证的对象将从池中删除销毁。默认 false
        genericObjectPoolConfig.setTestWhileIdle(ftpClientProperties.isTestWhileIdle());
        //对象空闲的最小时间，达到此值后空闲对象将可能会被移除
        genericObjectPoolConfig.setMinEvictableIdleTimeMillis(ftpClientProperties.getMinEvictableIdleTimeMillis());
        //如果没有获取到连接那么等待时间等待5秒
        genericObjectPoolConfig.setMaxWaitMillis(ftpClientProperties.getMaxWaitMillis());
        //向调用者输出“链接”资源时，是否检测是有有效，如果无效则从连接池中移除，并尝试获取继续获取。默认为false
        genericObjectPoolConfig.setTestOnBorrow(true);
        return genericObjectPoolConfig;
    }

    //@Bean
    //@Autowired
    public FtpClientTemplate getFtpTemplate(FtpClientFactory ftpClientFactory,GenericObjectPoolConfig genericObjectPoolConfig) {
        return new FtpClientTemplate(ftpClientFactory,genericObjectPoolConfig);
    }

}
